package com.cmc.cloud.cmclink.doc.controller.admin.si;

import java.util.Collection;import java.util.List;
import java.util.Map;import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.cmc.cloud.cmclink.doc.api.document.emums.CostTypeEnum;import com.cmc.cloud.cmclink.doc.util.CostChargeUtil;
import com.cmc.cloud.cmclink.doc.util.CostChargeWithBlnoUtil;import com.cmc.cloud.cmclink.doc.vo.blnochargevo.BlnoLocalChargeResultVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.api.document.emums.ExImTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.entity.BlnoCntrDO;
import com.cmc.cloud.cmclink.doc.service.BlnoCntrService;
import com.cmc.cloud.cmclink.doc.service.si.BlnoLocalCostExService;
import com.cmc.cloud.cmclink.doc.service.si.BlnoLocalCostImService;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostdetailvo.BlnoContainerSizeRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostdetailvo.BlnoContainerTypeRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostdetailvo.BlnoLocalCostDetailAllRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnolocalcostvo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 出口提单本地费用前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 出口提单本地费用")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "")
public class BlnoLocalCostExController {

    @Resource
    private BlnoLocalCostExService blnoLocalCostExService;

    @Resource
    private BlnoCntrService blnoCntrService;
    @Resource
    private BlnoLocalCostImService blnoLocalCostImService;

    @Resource
    private CostChargeWithBlnoUtil costChargeWithBlnoUtil;

    @Operation(summary = "获得所有提单列表")
    @PostMapping("/blnoLocalCost/list/bill")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost:list-bill')")
    @UseResponsePropertyAdvice
    public CommonResult<List<BlnoLocaCoastExRespVO>>
        getBlnoLocalCostList(@Valid @RequestBody BlnoLocalCostExReqVO reqVO) {
        List<BlnoLocaCoastExRespVO> list = blnoLocalCostExService.getBlnoWithLocalCostList(reqVO);
        return CommonResult.success(list);
    }

    @Operation(summary = "计算 Local 费用")
    @PostMapping("/blnoLocalCost/cal")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost:cal')")
    public CommonResult<List<BlnoLocalChargeResultVO>> calLocalCost(@Valid @RequestBody BlnoLocalCostCalIdsVO reqVO) {
        Map<Long, String> errMsgMap =
            blnoLocalCostImService.calLocalCostIm(reqVO.getIds(), null, ExImTypeEnum.EX.getValue(), reqVO.getCurrentPort());
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(reqVO.getIds(), errMsgMap, CostTypeEnum.LOCAL_COST_CAL_EX.getValue());
        return CommonResult.success(rspList);
    }

    @Operation(summary = "获得进口Local-Cost详情列表")
    @GetMapping("/blno-local-cost-detail/get/{blnoId}")
    @Parameter(name = "blnoId", description = "blnoId", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:queryBlnoId')")
    public CommonResult<BlnoLocalCostImAllRespVO> getBlnoLocalCostDetail(@PathVariable("blnoId") Long blnoId) {
        BlnoLocalCostImAllRespVO freightResp = blnoLocalCostImService.getBlnoAllLocalCost(blnoId, ExImTypeEnum.EX.getValue());
        return CommonResult.success(freightResp);
    }

    @Operation(summary = "发送财务")
    @PostMapping("/blnoLocalCost/account")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost:account')")
    public CommonResult<List<BlnoLocalChargeResultVO>> toAccount(@RequestParam("ids") Collection<Long> ids) {
        Map<Long, String> errMsgMap = blnoLocalCostImService.toAccountLocalCost(ids, ExImTypeEnum.EX.getValue());
        List<BlnoLocalChargeResultVO> rspList =
            costChargeWithBlnoUtil.transitMapToList(ids, errMsgMap, CostTypeEnum.LOCAL_COST_ACCOUNT_EX.getValue());
        return CommonResult.success(rspList);
    }

    @Operation(summary = "创建出Local-Cost费用")
    @PostMapping("/blno-local-cost-detail/create")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:create')")
    public CommonResult<Void> createBlnoLocalCostDetail(@Valid @RequestBody BlnoLocalCostExCreateReqVO createReqVO) {
        blnoLocalCostExService.createBlnoLocalCost(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "从本地费用明细复制")
    @PostMapping("/blno-local-cost-detail/copy")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:copy')")
    public CommonResult<Void> copyFromHistory(@Valid @RequestBody BlnoLocalCostExCreateReqVO createReqVO) {
        blnoLocalCostExService.createBlnoLocalCost(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新提单本地费用明细")
    @PostMapping("/blno-local-cost-detail/update")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:update')")
    public CommonResult<Void> updateBlnoLocalCostDetail(@Valid @RequestBody BlnoLocalCostExUpdateReqVO updateReqVO) {
        blnoLocalCostExService.updateBlnoLocalCost(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除提单本地费用明细")
    @PostMapping("/blno-local-cost-detail/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:delete')")
    public CommonResult<Void> deleteBlnoLocalCostDetail(@PathVariable("id") Long id) {
        blnoLocalCostExService.deleteBlnoLocalCost(id);
        return CommonResult.success();
    }

    @Operation(summary = "获取提单的箱型列表")
    @GetMapping("/blno-local-cost-detail/type/{blnoId}")
    @Parameter(name = "blnoId", description = "blnoId", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:type')")
    public CommonResult<List<BlnoContainerTypeRespVO>> getBlnoType(@PathVariable("blnoId") Long blnoId) {
        // 获取提单的箱型
        List<BlnoCntrDO> blnoCntrByBlnoId = blnoCntrService.getBlnoCntrByBlnoId(blnoId);

        // 筛选出提单的箱型
        List<BlnoContainerTypeRespVO> containerTypeRespVos = blnoCntrByBlnoId.stream().map(BlnoCntrDO::getCntrType)
            .distinct().map(cntrType -> BlnoContainerTypeRespVO.builder().typeCode(cntrType).build())
            .collect(Collectors.toList());

        return CommonResult.success(containerTypeRespVos);
    }

    @Operation(summary = "获取提单的箱尺寸列表")
    @GetMapping("/blno-local-cost-detail/size/{blnoId}")
    @Parameter(name = "blnoId", description = "blnoId", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-local-cost-detail:size')")
    public CommonResult<List<BlnoContainerSizeRespVO>> getBlnoSize(@PathVariable("blnoId") Long blnoId) {
        // 获取提单的箱型
        List<BlnoCntrDO> blnoCntrByBlnoId = blnoCntrService.getBlnoCntrByBlnoId(blnoId);

        List<BlnoContainerSizeRespVO> containerTypeRespVos = blnoCntrByBlnoId.stream().map(BlnoCntrDO::getCntrSize)
            .distinct().map(cntrSize -> BlnoContainerSizeRespVO.builder().sizeCode(cntrSize).build())
            .collect(Collectors.toList());

        return CommonResult.success(containerTypeRespVos);
    }
}
